------------------------------------
--	Simple Admin System
--	File: Core.lua
--	By Spacetech
--	License: http://creativecommons.org/licenses/by-nc-sa/3.0/
------------------------------------

function SAS.AddChatCommand(CommandName, Function, UserGroup, ArgsNumber)
	table.insert(SAS.Commands, {CommandName, Fuqnction, UserGroup, ArgsNumber})
end

function SAS.Nofity(ply, Text)
	ply:SendLua("GAMEMODE:AddNotify(\"SAS: "..Text.."\", NOTIFY_GENERIC, 5); surface.PlaySound(\"ambient/water/drip"..math.random(1, 4)..".wav\")")
	ply:PrintMessage(HUD_PRINTCONSOLE, "SAS: "..Text)
end

function SAS.NofityAll(Text)
	for k, v in pairs(player.GetAll()) do
		v:SendLua("GAMEMODE:AddNotify(\"SAS: "..Text.."\", NOTIFY_GENERIC, 5); surface.PlaySound(\"ambient/water/drip"..math.random(1, 4)..".wav\")")
		v:PrintMessage(HUD_PRINTCONSOLE, "SAS: "..Text)
	end
end

function SAS.Print(ply, Text)
	ply:PrintMessage(HUD_PRINTTALK, "SAS: "..Text)
end

function SAS.PrintAll(Text)
	for k, v in pairs(player.GetAll()) do
		v:PrintMessage(HUD_PRINTTALK, "SAS: "..Text)
	end
end

function SAS.NError(Text)
	for k, v in pairs(player.GetAll()) do
		if(SAS.AdminCheck(v, false)) then
			v:PrintMessage(HUD_PRINTCONSOLE, "----------------------------------------")
			v:PrintMessage(HUD_PRINTCONSOLE, "------------SAS Error Start-------------")
			v:PrintMessage(HUD_PRINTCONSOLE, "--------Please Report this Error--------")
			v:PrintMessage(HUD_PRINTCONSOLE, "----------------------------------------")
			v:PrintMessage(HUD_PRINTCONSOLE, Text)
			v:PrintMessage(HUD_PRINTCONSOLE, "----------------------------------------")
			v:PrintMessage(HUD_PRINTCONSOLE, "------------SAS Error End---------------")
			v:PrintMessage(HUD_PRINTCONSOLE, "----------------------------------------")
			v:SendLua("GAMEMODE:AddNotify(\"SAS Error: Please Check Your Console For The Error\", NOTIFY_GENERIC, 5); surface.PlaySound(\"ambient/water/drip"..math.random(1, 4)..".wav\")")
		end
	end
	Msg("----------------------------------------\n")
	Msg("------------SAS Error Start-------------\n")
	Msg("--------Please Report this Error--------\n")
	Msg("----------------------------------------\n")
	Msg(Text.."\n")
	Msg("----------------------------------------\n")
	Msg("------------SAS Error End---------------\n")
	Msg("----------------------------------------\n")
end

--Based on a function in SS by Conna
function SAS.LogEvent(Text)
	local Date = string.gsub(os.date("%x"), "/", "-")
	local FName = "SASLogs/"..Date..".txt"
	Text = "("..os.date("%X")..") "..Text
	if(file.Exists(FName)) then
		local LastText = file.Read(FName)
		Text = LastText..Text..".\n"
	else
		Text = Text..".\n"
	end
	file.Write(FName, Text)
end

function SAS.CheckPlayer(Nick)
	for k, v in pairs(player.GetAll()) do
		if(string.find(string.lower(v:Nick()), string.lower(Nick)) == 1) then
			return v
		end
	end
	return false
end

function SAS.AdminCheck(ply, Target)
	if(Target == false) then
		if(ply:IsSuperAdmin() || ply:IsAdmin() || ply:IsUserGroup("moderator") ) then
			return true
		else
			return false
		end
	else
		if(ply:IsSuperAdmin()) then
			return false
		elseif(ply:IsAdmin() && Target:IsSuperAdmin()) then
			return true
		elseif(ply:IsUserGroup("moderator") && (Target:IsAdmin() || Target:IsSuperAdmin())) then
			return true
		else
			return false
		end
	end
end

function SAS.ContinueCheck(ply, UserGroup)
	if(!ply:IsValid() || !ply:IsPlayer()) then
		return false
	end
	if(ply:IsSuperAdmin()) then
		return true
	elseif(ply:IsAdmin() && UserGroup == ("Admin" || "Moderator")) then
		return true
	elseif(ply:IsUserGroup("moderator") && UserGroup == "Moderator") then
		return true
	elseif(UserGroup == "") then
		return true
	else
		return false
	end
end

function SAS.ShowAdverts()
	if(SAS.Adverts != nil && table.Count(SAS.Adverts) != 0) then
		if(SAS.AdvertCounter > table.Count(SAS.Adverts)) then
			SAS.AdvertCounter = 1
		end
		SAS.NofityAll(SAS.Adverts[SAS.AdvertCounter])
		SAS.AdvertCounter = SAS.AdvertCounter + 1
	else
		timer.Remove(SAS.AdvertTimer)
	end
end
timer.Create("SAS.AdvertTimer", SAS.AdvertsTimer, 0, SAS.ShowAdverts)
